<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :inline="true" :model="searchMap" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.name"
          placeholder="景区关键字"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchMap.data.attractionsId" placeholder="请选择景区">
          <el-option label="请选择景区" value=""></el-option>
          <el-option
            v-for="item in scenicList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchData()">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="
            dialogFormVisible = true;
            ticket = {};
          "
          >添加门票</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="delAllIds()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      ref="multipleTable"
      :data="ticketList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="name" label="门票名称"> </el-table-column>
      <el-table-column prop="scenicName" label="景区名称"> </el-table-column>
      <el-table-column prop="price" label="门票价格"> </el-table-column>
      <el-table-column prop="num" label="门票数量"> </el-table-column>
      <el-table-column prop="type" label="门票类型">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">景区</span>
          <span v-if="scope.row.type == 1">机票</span>
          <span v-if="scope.row.type == 2">酒店</span>
        </template>
      </el-table-column>
      <el-table-column prop="spec" label="门票规格">
        <template slot-scope="scope">
          <span v-if="scope.row.spec == 0">儿童票</span>
          <span v-if="scope.row.spec == 1">成人票</span>
          <span v-if="scope.row.spec == 2">单间</span>
          <span v-if="scope.row.spec == 3">标准间</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block" style="float: right">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchMap.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </div>
    <!-- 添加对话框 -->
    <el-dialog title="景区门票" :visible.sync="dialogFormVisible">
      <el-form :model="ticket">
        <el-form-item label="选择景区">
          <el-select v-model="ticket.attractionsId" placeholder="请选择景区">
            <el-option
              v-for="item in scenicList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门票名称">
          <el-input v-model="ticket.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="门票价格">
          <el-input v-model="ticket.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="门票数量">
          <el-input v-model="ticket.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="门票类型">
          <el-select v-model="ticket.type" placeholder="请选择门票类型">
            <el-option label="景区" value="0"></el-option>
            <el-option label="机票" value="1"></el-option>
            <el-option label="酒店" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门票规格">
          <el-select v-model="ticket.spec" placeholder="请选择门票规格">
            <el-option label="儿童票" value="0"></el-option>
            <el-option label="成人票" value="1"></el-option>
            <el-option label="单间" value="2"></el-option>
            <el-option label="标准间" value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTicket()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  search,
  scenicSearch,
  addTicket,
  del,
  delAll,
} from "@/api/ticket";
export default {
  data() {
    return {
      searchMap: {
        data: {},
        page: 1,
        limit: 5,
      },
      ticketList: [], //门票list
      scenicList: [], //景区list
      total: 0,
      dialogFormVisible: false,
      ticket: {
        attractionsId: "",
      }, //门票entity
      delTicket: {}, //删除ticket
      ids:[],//批量删除id list
    };
  },
  methods: {
    //搜索
    searchData() {
      search(this.searchMap).then((res) => {
        if (res.success) {
          this.ticketList = res.data;
          this.total = res.total;
        }
      });
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.searchData();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.searchData();
    },
    //添加
    addTicket() {
      addTicket(this.ticket).then((res) => {
        if (res.success) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.searchData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible = false;
      });
    },
    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.ticket = row;
    },
    //删除
    handleDelete(index, row) {
      this.delTicket = row;
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        del(this.delTicket).then((res) => {
          if (res.success) {
            this.searchData();
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        });
      });
    },
    //批量删除
    handleSelectionChange(val) {
      this.ids=[]
      console.log(val)
      val.forEach(element => {

        this.ids.push(element.id)
        console.log(element.id)
      });
      console.log(this.ids)
    },
    delAllIds(){
       this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delAll(this.ids).then(res=>{
          if (res.success) {
              this.searchData();
              this.$message({
                showClose: true,
                message: res.msg,
                type: "success",
              });
            } else {
              this.$message({
                showClose: true,
                message: res.msg,
                type: "error",
              });
            }
        })
      });
      
    }
  },
  created() {
    this.searchData();
    scenicSearch().then((res) => {
      if (res.success) {
        this.scenicList = res.data;
      }
    });
  },
};
</script>